package com.gai.shop.web.controller;

import com.alibaba.fastjson.JSON;
import com.gai.shop.common.Const;
import com.gai.shop.entity.User;
import com.gai.shop.service.UserService;
import com.gai.shop.service.impl.UserServiceImpl;
import com.gai.shop.util.MD5Utils;
import com.gai.shop.util.StrUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

public class AdminController extends BaseController {
    private final UserService US = new UserServiceImpl();

    /**
     * 管理员登录
     * @param request
     * @param response
     * @return
     * @throws ServletException
     * @throws IOException
     */
    public String adminLogin(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String uname = request.getParameter("uname");
        String upassword = request.getParameter("upassword");
        User user = US.selectUserByUname(uname);
        if (user != null) {//有这个用户
            if (user.getUrole() != 1){//如果权限不足，不是管理员无法登录
                request.getSession().setAttribute(Const.MESSAGE,"只有管理员能够登录");
                return Const.REDIRECT + "/admin/login.jsp";
            }
            if (user.getUpassword().equals(MD5Utils.md5(upassword))){
                request.getSession().setAttribute(Const.AdminConst.ADMIN,user);//把登录管理员的数据进行保存
                return Const.REDIRECT + "/admin/admin.jsp";
            }
        }
        request.getSession().setAttribute(Const.MESSAGE,"用户名或密码错误");
        return Const.REDIRECT + "/admin/login.jsp";
    }

    /**
     * 注销登录
     * @param request
     * @param response
     * @return
     * @throws ServletException
     * @throws IOException
     */
    public String adminLogout(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.getSession().removeAttribute("admin");//清除session某个值
        return  Const.REDIRECT + "/admin/login.jsp";
    }

    /**
     *
     * @param request
     * @param response
     * @return
     * @throws ServletException
     * @throws IOException
     */
    public String getUserList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        /*
         * 查询条件有2个
         * uname ==> 模糊查询
         * usex ==> 查询
         * 1. uname like %s%
         */
        String uname = request.getParameter("uname");//
        String usex = request.getParameter("usex");//s
        //select * from user
        //select * from user 修改 为 select * from user where uname like %s%
        //select * from user and usex = usex
        List<User> userList = US.userList(uname,usex);
        String jsonString = JSON.toJSONString(userList);
        //写出响应
        return jsonString;
    }

    public String deleteUser(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Integer uid = Integer.valueOf(request.getParameter("uid"));
        Integer result = US.deleteUser(uid);
        if (result>0) {
            return "删除成功";
        }else {
            return "删除失败";
        }
    }
}
